<?php 

class Photo
{
	var $username = "tlvd";
	var $password = "n6f4y3";
	var $server = "localhost";
	var $port = "3306";
	var $databasename = "tlvd";
	
	var $connection;
	
	function __construct()
	{
		$this->connection = mysqli_connect($this->server, $this->username, $this->password, $this->databasename, $this->port);
	}
	
	private function throwExceptionOnError($link = null) { 
	    if($link == null) { 
	      $link = $this->connection; 
	    } 
	    if(mysqli_error($link)) { 
	    	echo mysqli_error($link);
	      $msg = mysqli_errno($link) . ": " . mysqli_error($link); 
	      throw new Exception('MySQL Error - '. $msg); 
	    }         
	  } 
	
	public function getTitleList()
	{
		$rows = array();
		mysqli_query($this->connection, "set names utf8");
		$sql = "select news.newsId, news.newsTitle, news.newsContent, news.lastModifyTime from news order by newsId asc";
		$stmt = mysqli_prepare($this->connection, $sql);
		mysqli_stmt_execute($stmt);
		$this->throwExceptionOnError();
		mysqli_stmt_bind_result($stmt, $row->newsId, $row->newsTitle, $row->newsContent, $row->lastModifyTime);
		
		while (mysqli_stmt_fetch($stmt)) 
		{
			$rows[] = $row;
			$row = new stdClass();
			mysqli_stmt_bind_result($stmt, $row->newsId, $row->newsTitle, $row->newsContent, $row->lastModifyTime);
		}
		
		mysqli_stmt_free_result($stmt);
		mysqli_close($this->connection);
		
		return $rows;     
	}
	
	public function getBrandListById($id)
	{
		$rows = array();
		mysqli_query($this->connection, "set names utf8");
		$sql = "select brand.brandId, brand.brandImg, brand.brandDesc, brand.typeId, brand.targetId from brand where brand.typeId = ". $id ." order by orderId asc";
		$stmt = mysqli_prepare($this->connection, $sql);
		mysqli_stmt_execute($stmt);
		$this->throwExceptionOnError();
		mysqli_stmt_bind_result($stmt, $row->brandId, $row->brandImg, $row->brandDesc, $row->typeId, $row->targetId);
		
		while (mysqli_stmt_fetch($stmt)) 
		{
			$rows[] = $row;
			$row = new stdClass();
			mysqli_stmt_bind_result($stmt, $row->brandId, $row->brandImg, $row->brandDesc, $row->typeId, $row->targetId);
		}
		
		mysqli_stmt_free_result($stmt);
		mysqli_close($this->connection);
		
		return $rows;     
	}
	
	public function getPhotoListByType($type, $start)
	{
		$rows = array();
		mysqli_query($this->connection, "set names utf8");
		$stmt = mysqli_prepare($this->connection, "select photo.photoId, photo.photoSmall, photo.photoBig, photo.photoDesc, photo.photoTypeId from photo where photoTypeId = ". $type ." ORDER BY orderId asc limit ". $start .", 8");
		mysqli_stmt_execute($stmt);
		$this->throwExceptionOnError();
		mysqli_stmt_bind_result($stmt, $row->photoId, $row->photoSmall, $row->photoBig, $row->photoDesc, $row->photoTypeId);
		
		while (mysqli_stmt_fetch($stmt)) 
		{
			$rows[] = $row;
			$row = new stdClass();
			mysqli_stmt_bind_result($stmt, $row->photoId, $row->photoSmall, $row->photoBig, $row->photoDesc, $row->photoTypeId);
		}
		
		mysqli_stmt_free_result($stmt);
		mysqli_close($this->connection);
		
		return $rows;     
	}
	
	public function getOfficeList($start)
	{
		$rows = array();
		mysqli_query($this->connection, "set names utf8");
		$stmt = mysqli_prepare($this->connection, "select office.officeId, office.officeSmall, office.officeBig from office limit ". $start .", 8");
		
		mysqli_stmt_execute($stmt);
		
		mysqli_stmt_bind_result($stmt, $row->officeId, $row->officeSmall, $row->officeBig);
		
		while (mysqli_stmt_fetch($stmt)) 
		{
			$rows[] = $row;
			$row = new stdClass();
			mysqli_stmt_bind_result($stmt, $row->officeId, $row->officeSmall, $row->officeBig);
		}
		
		mysqli_stmt_free_result($stmt);
		mysqli_close($this->connection);
		
		return $rows;     
	}
	
	public function getBrandList($start)
	{
		$rows = array();
		mysqli_query($this->connection, "set names utf8");
		$stmt = mysqli_prepare($this->connection, "select photo.photoId, photo.photoSmall, photo.photoBig, photo.photoDesc, photo.photoTypeId from photo where photoTypeId in (13, 14) ORDER BY orderId asc limit ". $start .", 8");
		
		mysqli_stmt_execute($stmt);
		
		mysqli_stmt_bind_result($stmt, $row->photoId, $row->photoSmall, $row->photoBig, $row->photoDesc, $row->photoTypeId);
		
		while (mysqli_stmt_fetch($stmt)) 
		{
			$rows[] = $row;
			$row = new stdClass();
			mysqli_stmt_bind_result($stmt, $row->photoId, $row->photoSmall, $row->photoBig, $row->photoDesc, $row->photoTypeId);
		}
		
		mysqli_stmt_free_result($stmt);
		mysqli_close($this->connection);
		
		return $rows;     
	}
	
	public function getFoodList($start)
	{
		$rows = array();
		mysqli_query($this->connection, "set names utf8");
		$stmt = mysqli_prepare($this->connection, "select photo.photoId, photo.photoSmall, photo.photoBig, photo.photoDesc, photo.photoTypeId from photo where photoTypeId in (10, 11, 12) ORDER BY orderId asc limit ". $start .", 8");
		
		mysqli_stmt_execute($stmt);
		
		mysqli_stmt_bind_result($stmt, $row->photoId, $row->photoSmall, $row->photoBig, $row->photoDesc, $row->photoTypeId);
		
		while (mysqli_stmt_fetch($stmt)) 
		{
			$rows[] = $row;
			$row = new stdClass();
			mysqli_stmt_bind_result($stmt, $row->photoId, $row->photoSmall, $row->photoBig, $row->photoDesc, $row->photoTypeId);
		}
		
		mysqli_stmt_free_result($stmt);
		mysqli_close($this->connection);
		
		return $rows;     
	}
	
	public function getPhotoListByTypeId($typeId, $start)
	{
		$rows = array();
		mysqli_query($this->connection, "set names utf8");
		$stmt = mysqli_prepare($this->connection, "select photo.photoId, photo.photoSmall, photo.photoBig, photo.photoDesc, photo.photoTypeId from photo where photoTypeId = ". $typeId ."  ORDER BY orderId asc limit ". $start .", 8");
		
		mysqli_stmt_execute($stmt);
		
		mysqli_stmt_bind_result($stmt, $row->photoId, $row->photoSmall, $row->photoBig, $row->photoDesc, $row->photoTypeId);
		
		while (mysqli_stmt_fetch($stmt)) 
		{
			$rows[] = $row;
			$row = new stdClass();
			mysqli_stmt_bind_result($stmt, $row->photoId, $row->photoSmall, $row->photoBig, $row->photoDesc, $row->photoTypeId);
		}
		
		mysqli_stmt_free_result($stmt);
		mysqli_close($this->connection);
		
		return $rows;     
	}
	
	public function getPhotoTypeList()
	{
		$rows = array();
		mysqli_query($this->connection, "set names utf8");
		$stmt = mysqli_prepare($this->connection, "select type.typeId, type.typeName, type.parentId, type.orderId from type order by orderId asc");
		
		mysqli_stmt_execute($stmt);
		
		mysqli_stmt_bind_result($stmt, $row->typeId, $row->typeName, $row->parentId, $row->orderId);
		
		while (mysqli_stmt_fetch($stmt)) 
		{
			$rows[] = $row;
			$row = new stdClass();
			mysqli_stmt_bind_result($stmt, $row->typeId, $row->typeName, $row->parentId, $row->orderId);
		}
		
		mysqli_stmt_free_result($stmt);
		mysqli_close($this->connection);
		
		return $rows;    
	}
	
	public function getPhotoCount($type)
	{
		$con = mysql_connect($this->server, $this->username, $this->password);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		mysql_select_db($this->databasename, $con);
		
		$result = mysql_query("SELECT count(*) FROM photo where photoTypeId = " . $type);
		$count;
		while($row = mysql_fetch_array($result))
		  {
			 $count =  $row[0];
		  }
		
		mysql_close($con);
		return $count;
	}
	
	public function getTeaCount()
	{
		$con = mysql_connect($this->server, $this->username, $this->password);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		mysql_select_db($this->databasename, $con);
		
		$result = mysql_query("SELECT count(*) FROM photo where photoTypeId in (6, 7, 8, 9)");
		$count;
		while($row = mysql_fetch_array($result))
		  {
			 $count =  $row[0];
		  }
		
		mysql_close($con);
		return $count;
	}
	
	public function getWineCount()
	{
		$con = mysql_connect($this->server, $this->username, $this->password);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		mysql_select_db($this->databasename, $con);
		
		$result = mysql_query("SELECT count(*) FROM photo where photoTypeId = 2");
		$count;
		while($row = mysql_fetch_array($result))
		  {
			 $count =  $row[0];
		  }
		
		mysql_close($con);
		return $count;
	}
	
	public function getFoodCount()
	{
		$con = mysql_connect($this->server, $this->username, $this->password);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		mysql_select_db($this->databasename, $con);
		
		$result = mysql_query("SELECT count(*) FROM photo where photoTypeId in (10, 11, 12)");
		$count;
		while($row = mysql_fetch_array($result))
		  {
			 $count =  $row[0];
		  }
		
		mysql_close($con);
		return $count;
	}
	
	public function getBrandCount()
	{
		$con = mysql_connect($this->server, $this->username, $this->password);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		mysql_select_db($this->databasename, $con);
		
		$result = mysql_query("SELECT count(*) FROM photo where photoTypeId in (13, 14)");
		$count;
		while($row = mysql_fetch_array($result))
		  {
			 $count =  $row[0];
		  }
		
		mysql_close($con);
		return $count;
	}
	
	public function getOfficeCount()
	{
		$con = mysql_connect($this->server, $this->username, $this->password);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		mysql_select_db($this->databasename, $con);
		
		$result = mysql_query("SELECT count(*) FROM office");
		$count;
		while($row = mysql_fetch_array($result))
		  {
			 $count =  $row[0];
		  }
		
		mysql_close($con);
		return $count;
	}
}

?>